package org.mspring.mlog.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mspring.mlog.dao.CatalogDao;
import org.mspring.mlog.entity.Catalog;
import org.mspring.mlog.entity.Link;
import org.mspring.mlog.entity.Post;
import org.springframework.stereotype.Repository;

@Repository
public class CatalogDaoImpl extends BaseDaoImpl implements CatalogDao {

	@Override
	public int getPageCount(Catalog link) {
		
        // 初始化连接
        SqlSession sqlSession = getSqlSession();
        // 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession
				.selectList(
						"catalogWidgetAct.getPageCount",
						link);
		
		// 断开连接
		sqlSession.close();
		
		if (null == list) {
			return 0;
		} else {
			return list.size();
		}
	}

	@Override
	public List<Catalog> getPage(Catalog link) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession.selectList(
				"catalogWidgetAct.getPage", link);

		// 断开连接
		sqlSession.close();

		return list;
	}

	@Override
	public void deleteCatalog(Catalog catalog) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.delete(
				"catalogWidgetAct.deleteCatalog", catalog);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public void createCatalog(Catalog catalog) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.delete(
				"catalogWidgetAct.createCatalog", catalog);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public Catalog getCatalogById(Catalog catalog) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession.selectList(
				"catalogWidgetAct.getCatalogById", catalog);

		// 断开连接
		sqlSession.close();
		
		if (null != list && list.size() > 0) {
			
			return (Catalog) list.get(0);
		}
		
		return null;

	}

	@Override
	public void updateCatalog(Catalog catalog) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		sqlSession.delete(
				"catalogWidgetAct.updateCatalog", catalog);

		// 断开连接
		sqlSession.close();
	}

	@Override
	public List<Catalog> catalogExists(Catalog catalog) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession.selectList(
				"catalogWidgetAct.catalogExists", catalog);

		// 断开连接
		sqlSession.close();

		return list;
	}

	@Override
	public List<Catalog> findAllCatalog(Post post) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<Catalog> list = (List<Catalog>) sqlSession.selectList(
				"catalogWidgetAct.findAllCatalog", post);

		// 断开连接
		sqlSession.close();

		return list;
	}

}
